package com.zzh.common;

import com.zzh.utils.IpUtil;
import com.zzh.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Objects;

/**
 * @author wenrouyue
 * @FileName: HttpRequestAspect
 * @description
 * @create 2023/02/11-20:11
 **/
@Aspect
@Component
@Slf4j
public class HttpRequestAspect implements Serializable {

    private static final long serialVersionUID = -5580237015363881780L;

    public static long startTime;
    public static long endTime;
    public String model;

    /**
     * PointCut注解表示表示横切点，哪些方法需要被横切
     */
    @Pointcut("execution(public * com.zzh.controller.*.*(..))")

    public void print() {

    }

    /**
     * Before注解表示在具体的方法之前执行
     *
     * @param joinPoint joinPoint
     */
    @Before("print()")
    public void before(JoinPoint joinPoint) {
        startTime = System.currentTimeMillis();
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert requestAttributes != null;
        HttpServletRequest request = requestAttributes.getRequest();
        Object[] args = joinPoint.getArgs();
        String inParam = "";
        if (!Objects.isNull(args)) {
            inParam = JsonUtil.toJSONString(args);
        }
        model = joinPoint.getSignature().toLongString();
        String ipAddr = IpUtil.getIpAddr(request);
        log.info("用户请求ip:{}", ipAddr);
        log.info("{}_入参:{}", model, inParam);
        String basePath = request.getServerName() + ":" + request.getServerPort();
        String uri = (request).getRequestURI();
        String setRequestUrl = request.getScheme() + "://" + basePath + uri;
        log.info(setRequestUrl);
        String userAgent = request.getHeader("User-Agent");
        log.info("请求头:{}", userAgent);
        if (!userAgent.contains("Safari") || !userAgent.contains("Trident") || !userAgent.contains("Firefox") || !userAgent.contains("Opera") || !userAgent.contains("Chrome") || !userAgent.contains("Maxthon") || !userAgent.contains("360") || !userAgent.contains("MetaSr") || !userAgent.contains("World")|| !userAgent.contains("Mozilla")) {
            log.info("不是浏览器访问");
            //todo 这里上线要放开
//            throw new MyException(ResultEnum.SystemException);
        }
    }

    /**
     * After注解表示在方法执行之后执行
     */
    @After("print()")
    public void after() {
    }

    /**
     * AfterReturning注解用于获取方法的返回值
     *
     * @param object 返回参数
     */
    @AfterReturning(pointcut = "print()", returning = "object")
    public void getAfterReturn(Object object) {
        endTime = System.currentTimeMillis() - startTime;

        String outParam = JsonUtil.toJSONString(object);
        log.info("{}_出参:{}", model, outParam);
    }

}

